home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 112 / EnigmaAmiga112CD.iso / dalla rivista / awnpipe / awnp / awnp-docs / guiop.doc < prev    next >
Text File  |  2000-05-14  |  6KB  |  95 lines

  1. This tutorial has two versions, Arexx and 'C'. The 'C' tutorial is after the Arexx one.
  2.  
  3. Before reading further, take a moment to run the tutorial so you understand what the program does.
  4.  
  5. TUTORIAL 2 ARexx
  6. ----------------
  7.  
  8.  Now that we know how to designed a GUI we want to be able to operate one. To follow this example you need a basic knowledge of ARexx. Have a look at the file Tutorial2.rx in the demos drawer.
  9.  
  10.  The program flow is straight forward. 3 simple steps.
  11.  
  12. 1. Default values are set. These are the values of the gadgets when the GUI opens.
  13.  
  14. 2. A routine (buildgui) is called to write the GUI definition to the pipe. (More on this routine later)
  15.  
  16. 3. Events are read from the pipe and processed until the stream of events ends.
  17.  
  18. BUILDGUI
  19.  
  20.  The 'buildgui' routine contains the information defining the GUI window and its gadgets. The data is sent line by line to the 'topipe' routine which sends the data to the pipe, checks for errors, and returns the GID returned by the pipe. (more on 'topipe' later.)
  21.  
  22.  For most lines sent with 'topipe' the response is ignored. For gadget definitions however the return value is stored so we can identify gadget events later on.
  23.  
  24. TOPIPE
  25.  
  26.  This routine takes a line of text and writes it to the pipe. The pipes response is read. If everything is ok the second parameter of the response is returned. This is the GID if a gadget is defined and a null string in most other cases. If an error occurs the problem line is output and the script exits.
  27.  
  28.  You should use this routine or one like it when writing your own script for AWNPipe. It will help avoid common errors and make debugging much easier.
  29.  
  30. EVENT HANDLING
  31.  
  32.  The main loop reads an event from the pipe and parses it into its parts. The first word of an event always tells the event type. This information is used to call a routine for that type of event.
  33.  
  34. CLOSE EVENT
  35.  
  36. If a close event is received write a little text and then exit the script.
  37.  
  38. GADGET EVENT
  39.  
  40.  We check the second word of the event to see which gadget sent the event. For most gadgets we store event information and return. If the cancel gadget is the cause of the event we output some text and exit. If it was the done gadget we output the information for each gadget and exit. If it was the reset gadget we close the pipe, then create the gui again to restore the default values.
  41.  
  42. MENU EVENT
  43.  
  44.  The second word of a menu event is the menu number, the third word id the item number, and the forth the subitem number. This information is used to determine what action should be taken in response to an event.
  45.  
  46.  Almost any GUI can be operated using this approach demonstrated in this tutorial. Use these functions as building blocks in your own scripts.
  47.  
  48. TUTORIAL 2 in C
  49. ----------------
  50.  
  51. The files tutorial2.c and tutorial2 can be found in in the demos drawer.
  52.  
  53.  Now that we know how to designed a GUI we want to be able to operate one. To follow this example you need a basic knowledge of 'C'. Have a look at the file Tutorial2.c in the demos drawer. We will examine each of the functions in detail since they will be useful building blocks for making other GUIs. There is only 1 interesting structure.
  54.  
  55. struct GUIpipe myGP
  56.  
  57. This structure is used to manage the GUI. It contains the GUIs file handle, error status,and read buffer. It as has storage for some useful data parsed from replies or events.
  58.  
  59.  int main( int argc, char *argv[]);
  60.  
  61.  The program flow is straight forward. Default values are set. We call a routine to build the GUI and operate the gui if we built it successfully. To operate the GUI we simply read events and react to them. Before we exit we make sure the GUIs filehandle is closed.
  62.  
  63.  int getline(struct GUIpipe * GP);
  64.  
  65.  We need to handle text one line at a time when operating the GUI. This routine reads the pipe making a sure a full line is available before returning. It will remove the previous line from the buffer first if there was one. We delimit the line with a null byte so we can use it as a string in other functions.
  66.  
  67.  __stdargs int topipe(struct GUIpipe * GP, UBYTE * data,...)
  68.  
  69.  Again and Again you will need to write a line to the pipe and read the response. This routine does that for you. It writes data to the pipe and then reads a reply from the pipe. The reply line is parsed, and an error is flagged if the first part of the reply is not 'ok'. We return the value of the second reply part if all is ok, return 0 if an error occurred.
  70.  
  71.  topipe allows Printf like formatting of data written to the pipe (VFPrintf is used). The regular Printf formats are supported. This is not needed in this example but it will be very useful as we expand the program in the other tutorials.
  72.  
  73.  int buildgui(struct GUIpipe * GP);
  74.  
  75.  All the information that defines the GUI is contained in this function. It opens a filehandle on the AWNPipe device and returns an error if this fails. The window and gadget definitions are written to the pipe and the returned gadget ID's are stored. If all gadgets are created ok the window is opened. The final error status of the GUI is returned.
  76.  
  77. int getevent(struct GUIpipe * GP);
  78.  
  79.  This routine reads a line from the pipe and parses it storing information about the event. It returns the error status of the pipe.
  80.  
  81.  int gadgets(struct GUIpipe * GP);
  82.  int menu(struct GUIpipe * GP);
  83.  
  84.  These routines look at the information parsed from the last event and take appropriate action depending which gadget or menu item was selected. Some times the gadget state information from the event is stored. Other times information is output to the user.
  85.  
  86.  int gperror(struct GUIpipe * GP,int error);
  87.  
  88.  When an error is detected while reading or writing to the pipe this routine is called. It sets the error status and alerts the user an error has occurred.
  89.  
  90.  UBYTE * eventstr(struct GUIpipe * GP, int num);
  91.  
  92. Some fields in events , like string gadget contents, can contain spaces. This functions returns a pointer to the 'num' word of the event. Since the line is delimited by a null you can treat this as a string pointer. If the 'num' word is not found NULL is returned. The pointer is only valid until the next call to getline(), getevent(), or topipe().
  93.  
  94.  Almost any GUI can be operated using this approach demonstrated in this tutorial.
  95.